﻿using UnityEngine;
using System.Collections;

public class CandyNeighbor : MonoBehaviour
{
    private GameManager manager;
    public GameObject clicked_candy_, target_candy;
    public Vector3 from_pos, target_pos;
    public float start_time,swap_time;
    public static CandyNeighbor instance;
	// Use this for initialization
	void Start () {
        instance = this;
           manager = GameManager.getInstance();
	}

    private GameObject FindTargetCandy(GameObject obj, Move dir)
    {
		if(obj==null){return null;}
		target_candy = obj.GetComponent<CandyController>().GetNeighbor(dir.ToString());
        return target_candy;
    }

    public void FindNearByToMove()
	{
	    target_candy = FindTargetCandy(clicked_candy_, manager.Direction);
        if (target_candy != null)
        {
           	CandyDrop.instance.KinematicCandy(true);
            from_pos = clicked_candy_.transform.position;
            target_pos = target_candy.transform.position;
            CandyMovement.instance.Invoke("Moving",0.1f);
			return;
        }
		Invoke("ChangeState",0.2f);
    }
	private void ChangeState(){
		manager.gamestate=GameState.playing;
}
}
